/*
Copyright 2008, 2009 Joonas Lehtolahti, Jordan Toor

This file is part of Openfire-C.

Openfire-C is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Openfire-C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Openfire-C.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "XfirePackets.h"

XFIREPHO XfirePHc_AddFriend(non0string *username, non0string *text) {

	struct XfirePacket *p;
	
	if (!username) return NULL;
	
	p = XfirePHo_Init(0x0006, 2, 0);
	
	if (!text) text = char2non((unsigned const char *)"");
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"name", XFP_TYPE_STRING, username);
	XfirePHo_AddAttribute(p, (unsigned const char *)"msg", XFP_TYPE_STRING, text);

	return p;

}

XFIREPHO XfirePHc_AcceptFriend(non0string *username) {
	
	struct XfirePacket *p;
	
	if (!username) return NULL;
	
	p = XfirePHo_Init(0x0007, 1, 0);

	XfirePHo_AddAttribute(p, (unsigned const char *)"name", XFP_TYPE_STRING, username);

	return p;
}

XFIREPHO XfirePHc_RejectFriend(non0string *username) {

	struct XfirePacket *p;
	
	if (!username) return NULL;
	
	p = XfirePHo_Init(0x0008, 1, 0);

	XfirePHo_AddAttribute(p, (unsigned const char *)"name", XFP_TYPE_STRING, username);

	return p;
}

XFIREPHO XfirePHc_DeleteFriend(unsigned int userid) {

	struct XfirePacket *p;
	p = XfirePHo_Init(0x0009, 1, 0);

	XfirePHo_AddAttribute(p, (unsigned const char *)"userid", XFP_TYPE_INT, &userid);
	
	return p;
}

XFIREPHO XfirePHc_SearchFriends(non0string *term) {
	struct XfirePacket *p;
	non0string *firstname;
	non0string *lastname;
	non0string *email;
	if (!term) return NULL;
	
	p = XfirePHo_Init(0x000c, 4, 0);
	
	firstname = char2non((unsigned const char *)"");
	lastname = char2non((unsigned const char *)"");
	email = char2non((unsigned const char *)"");
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"name", XFP_TYPE_STRING, term);
	XfirePHo_AddAttribute(p, (unsigned const char *)"fname", XFP_TYPE_STRING, firstname);
	XfirePHo_AddAttribute(p, (unsigned const char *)"lname", XFP_TYPE_STRING, lastname);
	XfirePHo_AddAttribute(p, (unsigned const char *)"email", XFP_TYPE_STRING, email);

	return p;
}

XFIREPHO XfirePHc_ChangeNick(non0string *nickname) {
	struct XfirePacket *p;
	if (!nickname) return NULL;
	
	p = XfirePHo_Init(0x000e, 1, 0);
	
	XfirePHo_AddAttribute(p, (unsigned const char *)"nick", XFP_TYPE_STRING, nickname);

	return p;
}

/*
 * XfirePHc_GameStatus
 *   Creates network packet for game playing status changing
 * 
 * Parameters:
 *   gameid    The identification number of the game
 *   gameip    The IP address of the server
 *   gameport  The port of the game server
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_GameStatus(unsigned int gameid,
		unsigned int gameip, unsigned int gameport) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Packet '04 00', has 3 attributes and regular attribute names */
	p = XfirePHo_Init(0x0004, 3, 0);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, (unsigned const char *)"gameid", XFP_TYPE_INT, &gameid);
	XfirePHo_AddAttribute(p, (unsigned const char *)"gip", XFP_TYPE_INT, &gameip);
	XfirePHo_AddAttribute(p, (unsigned const char *)"gport", XFP_TYPE_INT, &gameport);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_FriendGroupRename
 *   Creates network packet for renaming a custom friend group
 * 
 * Parameters:
 *   groupid   The identification number of the group
 *   name      The new name for the group
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_FriendGroupRename(unsigned int groupid,
		non0string *name) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Required parameters */
	if (!name) return NULL;
	
	/* Packet '1c 00', has 2 attributes and *short* attribute names */
	p = XfirePHo_Init(0x001c, 2, 1);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, SHORT_ATTRIBUTE(\x19), XFP_TYPE_INT, &groupid);
	XfirePHo_AddAttribute(p, SHORT_ATTRIBUTE(\x1a), XFP_TYPE_STRING, name);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_CustomStatus
 *   Creates network packet for setting a custom status
 * 
 * Parameters:
 *   text    the status text to set
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_CustomStatus(non0string *text) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Packet '20 00', has 1 attribute and *short* attribute names */
	p = XfirePHo_Init(0x0020, 1, 1);
	
	/* Default values for parameters */
	if (!text) text = char2non((unsigned const char *)"");
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, SHORT_ATTRIBUTE(\x2e), XFP_TYPE_STRING, text);
	
	/* Return completed packet */
	return p;
}

/*
 * XfirePHc_RequestFriendData
 *   Creates network packet for requesting data of a friend such as
 *   avatar image, uploaded screenshots and associated clans
 * 
 * Parameters:
 *   userid    the user id number
 * 
 * Returns:
 *   pointer to XfirePacket structure holding the data
 */
XFIREPHO XfirePHc_RequestFriendData(unsigned int userid) {
	/* Needed variables */
	struct XfirePacket *p;
	
	/* Packet '25 00', has 1 attribute and *short* attribute names */
	p = XfirePHo_Init(0x0025, 1, 1);
	
	/* Add attributes */
	XfirePHo_AddAttribute(p, SHORT_ATTRIBUTE(\x01), XFP_TYPE_INT, &userid);
	
	/* Return completed packet */
	return p;
}
